package com.hurt.competition.admin.controller;

import com.hurt.competition.admin.dto.NoticeReq;
import com.hurt.competition.admin.model.Notice;
import com.hurt.competition.admin.service.NoticeService;
import com.hurt.competition.common.util.CoreResultBuilder;
import com.hurt.competition.common.vo.CoreResult;
import com.hurt.competition.common.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/notice")
@Slf4j
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 查询通知列表
     * @param req
     * @return
     */
    @PostMapping("/list")
    public CoreResult list(@RequestBody NoticeReq req){
        log.info("list--------");
        PageResult<Notice> page = noticeService.queryList(req);
        return CoreResultBuilder.success(page);
    }

    /**
     * 查询通知详情
     * @param req
     * @return
     */
    @PostMapping("/info")
    public CoreResult info(@RequestBody NoticeReq req){
        if(req.getId() == null){
            return CoreResultBuilder.error("参数有误，请修改后重试");
        }
        Notice info = noticeService.queryInfo(req);
        return CoreResultBuilder.success(info);
    }

    /**
     * 发布通知
     * @param notice
     * @return
     */
    @PostMapping("/save")
    public CoreResult save(@RequestBody Notice notice){
        noticeService.save(notice);
        return CoreResultBuilder.success();
    }

    /**
     * 修改通知
     * @param notice
     * @return
     */
    @PostMapping("/update")
    public CoreResult update(@RequestBody Notice notice){
        if(notice.getId() == null){
            return CoreResultBuilder.error("参数有误，请修改后重试");
        }
        noticeService.updateById(notice);
        return CoreResultBuilder.success();
    }

    /**
     * 删除通知
     * @param notice
     * @return
     */
    @PostMapping("/delete")
    public CoreResult delete(@RequestBody Notice notice){
        if(notice.getId() == null){
            return CoreResultBuilder.error("参数有误，请修改后重试");
        }
        noticeService.removeById(notice);
        return CoreResultBuilder.success();
    }
}
